<?php

namespace app\api\controller;

use app\api\model\UserCar;
use think\Db;
use think\Request;
use app\api\model\Car as CarModel;
use app\api\model\VolleyDesc as Balance;
use app\api\model\ConfigSystem as Config;
use app\api\model\OrderCommon;

class Car extends Common
{
    public function lists(Request $request = null)
    {
        $data = Db::name('car')->where(['is_hidden' => 0])->select();

        $contain = [];
        foreach ($data as $item) {
            $item['price'] = explode(',', $item['price']);

            if ($item['types'] == 1) {
                $contain[1][] = $item;
            } else {
                $contain[2][] = $item;
            }
        }

        $contain = array_values($contain);

        return $this->pass($contain);
    }

    public function buy()
    {
        $data = CarModel::get($this->params['id']);

        if (!$data || $data['is_hidden']) return $this->return['common.error.notExist'];

        $price = explode(',',$data['price']);
        $price = $price[$this->params['expire']];

        $time = [1, 3, 12];

        if ($this->params['method'] == 1) {
            if ($price > $this->member['volley']) {
                return $this->return['user.error.insufficientBalance'];
            }

            $userCar = UserCar::get(['car_id' => $this->params['id'], 'user_id' => $this->member['id']]);

            if ($userCar) {
                $expire = $userCar['expire'] > time() ? $userCar['expire'] : time();
                $expire = $expire + $time[$this->params['expire']] * 86400;

                UserCar::update(['id' => $userCar['id'], 'expire' => $expire, 'update_time' => time()]);
            } else {
                $expire = time() + $time[$this->params['expire']] * 86400;

                UserCar::create(['car_id' => $this->params['id'], 'user_id' => $this->member['id'], 'expire' => $expire, 'create_time' => time(), 'update_time' => time()]);
            }

            Balance::index($this->member['id'], $price, '购买座驾');
            OrderCommon::index(5, $this->member['id'], $price, $this->params['method'], json_encode(['id' => $this->params['id'], 'expire' => $this->params['expire']]), 1);

            return $this->return['common.success.default'];
        } else {
            $config = Config::index('', 'rate');

            //$recharge = round($price / $config['rate'], 2);
            $recharge = round($price, 2);

            $order = OrderCommon::index(5, $this->member['id'], $price, $this->params['method'], json_encode(['id' => $this->params['id'], 'expire' => $this->params['expire']]));

            if (isset($this->params['h5']) && $this->params['h5']) {
                //$action = 'api/Alipaywap/tradeAppPay';
                return $this->pass(['order_sn' => $order['order_sn'], 'url' => 'api/wap/pay']);
            } else {
                $action = $this->params['method'] == 3 ? 'api/Wechat/app' : 'api/Alipay/tradeAppPay';
            }

            return action($action, ['order_sn' => $order['order_sn'], 'total_amount' => $recharge]);
        }
    }

    public function myCar(Request $request = null)
    {
        $page = $request->param('page') ?: 1;

        $data = Db::name('user_car t1')
            ->field('t1.id, t1.car_id, t1.expire, t1.is_select, t2.name, t2.cover, t2.price')
            ->join('car t2', 't2.id = t1.car_id')
            ->where(['t1.user_id' => $this->member['id']])
            ->order('t1.id desc')->page($page, 10)->select();

        foreach ($data as $key => $item) {
            $data[$key]['is_expire'] = $item['expire'] < time() ? 1 : 0;
            $data[$key]['expire'] = date('Y-m-d H:i:s', $item['expire']);

            $data[$key]['price'] = explode(',', $item['price']);
        }

        return $this->pass($data);
    }

    public function select()
    {
        UserCar::where(['id' => ['neq', $this->params['id']], 'user_id' => $this->member['id'], 'is_select' => 1])->setField('is_select', 0);

        UserCar::where(['id' => $this->params['id'], 'user_id' => $this->member['id']])->setField('is_select', 1);

        return $this->return['common.success.update'];
    }
}
